package com.example.m6_activity_lifecycle

import android.app.Activity
import android.content.Intent
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.IntentCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.transition.Visibility
import com.example.m6_activity_lifecycle.databinding.ActivityMainBinding
import com.example.m6_activity_lifecycle.models.UserInfo

class MainActivity : AppCompatActivity() {
    companion object {
        const val EXTRA_HAVE_MIDDLE_NAME = "EXTRA_HAVE_MIDDLE_NAME"
        const val EXTRA_USER_INFO = "EXTRA_USER_INFO"
        const val EMPTY_FIELD = "Пожалуйста заполните все поля"
        const val LOG_NAME = "ДЗ6"
    }

    private val userInfoLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
            when (result.resultCode) {
                Activity.RESULT_OK -> {
                    val intentData: Intent? = result.data
                    val userInfo: UserInfo = intentData?.let {
                        IntentCompat.getParcelableExtra(
                            it, EXTRA_USER_INFO, UserInfo::class.java
                        )
                    } ?: return@registerForActivityResult
                    val resultText: String =
                        "${userInfo.firstName} ${userInfo.lastName} ${userInfo.middleName}"
                    binding.userInfoTextView.text = resultText
                    binding.userInfoTextView.visibility = View.VISIBLE
                }
            }
        }
    private lateinit var binding: ActivityMainBinding
    private var haveMiddleName: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.haveMiddleNameCheckBox.setOnCheckedChangeListener { buttonView, isChecked ->
            haveMiddleName = isChecked
        }
        haveMiddleName = savedInstanceState?.getBoolean(EXTRA_HAVE_MIDDLE_NAME) ?: false
        binding.haveMiddleNameCheckBox.isChecked = haveMiddleName
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        binding.introduceButton.setOnClickListener {
            var intent = Intent(this, UserInfoActivity::class.java)
            intent.putExtra(EXTRA_HAVE_MIDDLE_NAME, haveMiddleName)
            userInfoLauncher.launch(intent)
        }
        Log.d(LOG_NAME, "MainActivity created")
    }

    override fun onStart() {
        super.onStart()
        Log.d(LOG_NAME, "MainActivity started")
    }

    override fun onResume() {
        super.onResume()
        Log.d(LOG_NAME, "MainActivity resumed")
    }

    override fun onPause() {
        super.onPause()
        Log.d(LOG_NAME, "MainActivity paused")
    }

    override fun onStop() {
        super.onStop()
        Log.d(LOG_NAME, "MainActivity stopped")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(LOG_NAME, "MainActivity destroyed")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        Log.d(LOG_NAME, "MainActivity onSaveInstanceState executed")
        super.onSaveInstanceState(outState)
        outState.putBoolean(EXTRA_HAVE_MIDDLE_NAME, haveMiddleName)
    }
}
//Result of all activity lifecycles
/*
MainActivity created
MainActivity started
MainActivity resumed
MainActivity paused
UserInfoActivity created
UserInfoActivity started
UserInfoActivity resumed
MainActivity stopped
UserInfoActivity paused
MainActivity started
MainActivity resumed
UserInfoActivity stopped
UserInfoActivity destroyed
*/